package com.jia.quick.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.quick.entity.User;
import com.jia.quick.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dujiajia
 * @since 2021-01-23
 */
@RestController
@RequestMapping("/quick/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("list")
    public List<User> list() {
        List<User> list = userService.list(null);
        return list;
    }
    @GetMapping("add")
    public int add(){
        User user = new User();
        user.setAge(18);
        user.setEmail("982255@qq.com");
        user.setName("狂神22222");
        userService.save(user);
        return 1;
    }

    @GetMapping("update")
    public int update(){
        User user = new User();
        user.setId(1352937088781246466L);
        user.setAge(20);
        user.setEmail("9822@qq.com");
        user.setName("狂神11111");
        userService.updateById(user);
        return 1;
    }

    @GetMapping("page/{current}/{size}")
    public List<User> page(@PathVariable int current,@PathVariable int size){
        Page<User> page = new Page<>(current,size);

        IPage<User> userIPage = userService.page(page, null);

        return userIPage.getRecords();
    }

    @GetMapping("delete/{id}")
    public int delete(@PathVariable Long id){
        userService.removeById(id);
        return 1;
    }
}

